package com.kefu.sys.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.vo.DeleteVo;
import com.kefu.common.vo.DetailVo;
import com.kefu.sys.model.SysMenu;
import com.kefu.sys.model.SysRoleMenu;
import com.kefu.sys.model.SysTenant;
import com.kefu.sys.service.SysMenuService;
import com.kefu.sys.service.SysRoleMenuService;
import com.kefu.sys.service.SysTenantService;
import com.kefu.sys.vo.SysMenuInsertVo;
import com.kefu.sys.vo.SysMenuUpdateVo;
import com.kefu.sys.vo.SysTenantVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags="菜单")
@RestController
@RequestMapping("sys/menu")
public class SysMenuController extends AbstractBaseController {

    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    SysRoleMenuService sysRoleMenuService;


    @PostMapping("/menuList")
    @ApiOperation(value = "查询所有菜单")
    public Result<List<?>> menuList() {

        List<?> list = sysMenuService.listAllTree();
        return ResultGenerator.genSuccessResult(list);
    }

    @PostMapping("/tenantMenu")
    @ApiOperation(value = "查询租户菜单", notes = "查询租户菜单")
    public Result<List<SysMenu>> tenantMenu(@Validated @RequestBody SysTenantVo vo) {
        return ResultGenerator.genSuccessResult(sysMenuService.getMenuByRoleId(vo.getTenantId()));
    }

    @PostMapping("/menuAdd")
    @ApiOperation(value = "新增功能菜单", notes = "新增功能菜单")
    public Result<String> menuAdd(@Validated @RequestBody SysMenuInsertVo sysMenuVo) {
        logger.info("新增功能菜单:{}", sysMenuVo);

        SysMenu info = sysMenuVo.convert();
        info.setWeight(sysMenuService.getMaxWeight(info.getParentId())+1);
        sysMenuService.save(info);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/menuDelete")
    @ApiOperation(value = "删除功能菜单", notes = "页面判断如果有下级菜单，不让删除")
    public Result<String> delete(@Validated @RequestBody DeleteVo delete) {
        logger.info("删除功能菜单:{}", delete);

        sysMenuService.deleteById(delete.getId());
        sysRoleMenuService.delete(Wrappers.lambdaQuery(SysRoleMenu.class).eq(SysRoleMenu::getMenuId,delete.getId()));
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/menuUpdate")
    @ApiOperation(value = "修改功能菜单", notes = "修改功能菜单")
    public Result<String> update(@Validated @RequestBody SysMenuUpdateVo sysMenuVo) {
        logger.info("修改功能菜单:{}", sysMenuVo);

        SysMenu info = sysMenuVo.convert();
        sysMenuService.update(info);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/menuDetail")
    @ApiOperation(value = "功能菜单详情", notes = "功能菜单详情")
    public Result<SysMenu> menuDetail(@Validated @RequestBody DetailVo vo) {

        return ResultGenerator.genSuccessResult(sysMenuService.getById(vo.getId()));
    }


}
